package com.lianniu.web.tea.order.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpResponse;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.order.model.TeaOrder;
import com.lianniu.web.tea.order.service.ITeaOrderService;
import com.lianniu.web.util.HttpUtils;

@Controller
@RequestMapping("/rest/tea/order")
public class TeaOrderController {
	
	private final static Logger logger = Logger.getLogger(TeaOrderController.class);
	
	@Autowired
	private ITeaOrderService teaOrderService;
	
	/**
	 * @return
	 */
	@RequestMapping(value="/getOrderInfoBefore", method = RequestMethod.GET)
	public @ResponseBody BaseResult getOrderInfoBefore(HttpServletRequest req){
		logger.info("TeaOrderController  getOrderInfoBefore begin.");
		try {
			String userId = req.getParameter("userId");
			String productId = req.getParameter("productId");
			logger.info("getOrderInfoBefore begin.");
			BaseResult baseResult = teaOrderService.getOrderInfoBefore(userId,productId);
			logger.info("getOrderInfoBefore end.");
			logger.info("TeaOrderController getOrderInfoBefore end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController getOrderInfoBefore error:" + e.getMessage());
			logger.error("TeaOrderController getOrderInfoBefore end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/getOrderInfo", method = RequestMethod.GET)
	public @ResponseBody BaseResult getOrderInfo(HttpServletRequest req){
		logger.info("TeaOrderController  getOrderInfo begin.");
		try {
			String orderId = req.getParameter("orderId");
			logger.info("getOrderInfo begin.");
			BaseResult baseResult = teaOrderService.getOrderInfo(orderId);
			logger.info("getOrderInfo end.");
			logger.info("TeaOrderController getOrderInfo end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController getOrderInfo error:" + e.getMessage());
			logger.error("TeaOrderController getOrderInfo end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/saveOrder", method = RequestMethod.POST)
	public @ResponseBody BaseResult saveOrder(HttpServletRequest req,@RequestBody TeaOrder order){
		logger.info("TeaOrderController  saveOrder begin.");
		try {
			logger.info("saveOrder begin.");
			BaseResult baseResult = teaOrderService.saveOrder(order);
			logger.info("saveOrder end.");
			logger.info("TeaOrderController saveOrder end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController saveOrder error:" + e.getMessage());
			logger.error("TeaOrderController saveOrder end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/cancleOrder", method = RequestMethod.GET)
	public @ResponseBody BaseResult cancleOrder(HttpServletRequest req){
		logger.info("TeaOrderController  cancleOrder begin.");
		try {
			logger.info("cancleOrder begin.");
			String orderId = req.getParameter("orderId");
			String status = req.getParameter("status");
			BaseResult baseResult = teaOrderService.cancleOrder(orderId,status);
			logger.info("cancleOrder end.");
			logger.info("TeaOrderController cancleOrder end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController cancleOrder error:" + e.getMessage());
			logger.error("TeaOrderController cancleOrder end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 根据用户id查询订单信息
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/getOrderListByUserId", method = RequestMethod.GET)
	public @ResponseBody BaseResult getOrderListByUserId(HttpServletRequest req){
		logger.info("TeaOrderController  getOrderListByUserId begin.");
		try {
			String userid = req.getParameter("userId");
			String status = req.getParameter("status");
			HashMap<String, String> param = new HashMap<String, String>();
			param.put("userid", userid);
			param.put("status", status);
			logger.info("getOrderListByUserId begin.");
			BaseResult baseResult = teaOrderService.getOrderListByUserId(param);
			logger.info("getOrderListByUserId end.");
			logger.info("TeaOrderController getOrderListByUserId end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController getOrderListByUserId error:" + e.getMessage());
			logger.error("TeaOrderController getOrderListByUserId end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 确认收货
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/makeSure", method = RequestMethod.GET)
	public @ResponseBody BaseResult makeSure(HttpServletRequest req){
		logger.info("TeaOrderController  makeSure begin.");
		try {
			String orderId = req.getParameter("orderId");
			logger.info("makeSure begin.");
			BaseResult baseResult = teaOrderService.makeSure(orderId);
			logger.info("makeSure end.");
			logger.info("TeaOrderController makeSure end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController makeSure error:" + e.getMessage());
			logger.error("TeaOrderController makeSure end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 查询物流
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/logistics", method = RequestMethod.GET)
	public @ResponseBody BaseResult logistics(HttpServletRequest req){
		logger.info("TeaOrderController  logistics begin.");
		try {
			String wayno = req.getParameter("wayno");
			String host = "http://jisukdcx.market.alicloudapi.com";
		    String path = "/express/query";
		    String method = "GET";
		    String appcode = "81212a84de3a46cdaa554958bf2b4027";
		    Map<String, String> headers = new HashMap<String, String>();
		    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		    headers.put("Authorization", "APPCODE " + appcode);
		    Map<String, String> querys = new HashMap<String, String>();
		    querys.put("number", wayno);
		    querys.put("type", "auto");
		    HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	String string = EntityUtils.toString(response.getEntity());
			return BaseResult.success(string);
		} catch (Exception e) {
			logger.error("TeaOrderController logistics error:" + e.getMessage());
			logger.error("TeaOrderController logistics end.");
			return BaseResult.exception(e.getMessage());
		}
	}

}
